package com.jiuyou.websocket.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;

public class StringUtil {

    private final Logger log = LoggerFactory.getLogger(StringUtil.class);

    public static String toString(Object o) {
        if (o == null) {
            return "";
        } else {
            return o.toString();
        }
    }

    public static String toString(Object o, String defaultValue) {
        if (o == null) {
            return defaultValue;
        } else {
            return o.toString();
        }
    }


    /**
     * ID生成器
     *
     * @param moduleName 模块简称 如（mb） 长度2 位
     * @return
     */
    public static String idGenerator(String moduleName) {
        StringBuffer sb = new StringBuffer("");
        if (StringUtils.isNotBlank(moduleName)) {
            if (moduleName.length() >= 2) {
                sb.append(moduleName.substring(0, 2));
            } else {
                sb.append(StringUtils.leftPad(moduleName, 2, "AB"));
            }
        } else {
            sb.append(StringUtils.leftPad(moduleName, 2, "AB"));
        }
        sb.append(timeString());
        sb.append(randString(3));
        sb.append(randNumber(4));
        return sb.toString();
    }

    /**
     * 生成系统时间字符串
     *
     * @return
     */
    public static String timeString() {
        StringBuffer sb = new StringBuffer("");
        sb.append(new DateTime().toString("yyyyMMddHHmmss"));
        String temp = sb.toString();
        Random r = new Random();
        StringBuffer sbTemp = new StringBuffer("");
        int range = temp.length();
        for (int i = 0; i < range; i++) {
            sbTemp.append(temp.charAt(i));
        }
        sbTemp.append(r.nextInt(10)).append(r.nextInt(10)).append(r.nextInt(10));
        return sbTemp.toString();
    }

    /**
     * 生成c位随即字符串
     *
     * @return
     */
    public static String randString(int c) {
        StringBuffer buffer = new StringBuffer("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        StringBuffer sb = new StringBuffer("");
        Random r = new Random();
        int range = buffer.length();
        for (int i = 0; i < c; i++) {
            sb.append(buffer.charAt(r.nextInt(range)));
        }
        return sb.toString();
    }

    public static String shortId() {
        StringBuffer sb = new StringBuffer("");
        DateTime dateTime = new DateTime();
        sb.append(dateTime.toString("yyMMddHHmmss"));
        sb.append(randString(3));
        sb.append(randString(3));
        return sb.toString();
    }

    /**
     * 随机3位数字
     *
     * @return
     */
    public static String randNumber(int c) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < c; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }

    /**
     * 时间戳+4位随机数组成16位字符串（唯一）
     * yyMMddHHmmss****
     *
     * @return
     */
    public static String nonceStr16() {
        StringBuffer sb = new StringBuffer("");
        DateTime dateTime = new DateTime();
        sb.append(dateTime.toString("yyMMddHHmmss"));
        sb.append(randNumber(4));
        return sb.toString();
    }

    /**
     * * 判断一个对象数组是否为空
     *
     * @param objects 要判断的对象数组
     ** @return true：为空 false：非空
     */
    public static boolean isEmpty(Object[] objects)
    {
        return isNull(objects) || (objects.length == 0);
    }

    /**
     * * 判断一个对象数组是否非空
     *
     * @param objects 要判断的对象数组
     * @return true：非空 false：空
     */
    public static boolean isNotEmpty(Object[] objects)
    {
        return !isEmpty(objects);
    }

    /**
     * * 判断一个对象是否为空
     *
     * @param object Object
     * @return true：为空 false：非空
     */
    public static boolean isNull(Object object)
    {
        return object == null;
    }

    /**
     * * 判断一个对象是否非空
     *
     * @param object Object
     * @return true：非空 false：空
     */
    public static boolean isNotNull(Object object)
    {
        return !isNull(object);
    }

    /**
     * 根据所传的字符串数组生成唯一字符串
     * @param separator 分隔符
     * @param strings 字符串数组
     * @return
     */
    public static String generateOnlyByArrStr(String separator, String ...strings) {
        String str = StringUtils.join(strings, separator);
        return DigestUtils.md5Hex(str);
    }

    public static void main(String[] args) {
        System.out.println(StringUtils.reverse(timeString().substring(7)));
    }
}
